import java.io.File;
import java.util.Random;

public class Shuffle {

	String separator = "#";
	boolean recursive = false;

	public Shuffle(String[] args) {

		if (args.length < 1) {
			System.err.println("Shuffle.class [-r][-u] dirpath");
			System.exit(1);
		}

		boolean untag=false;
		for (int i = 0; i < args.length; i++) {
			if (args[i].equals("-r")) recursive=true;
			if (args[i].equals("-u")) untag=true;
		}
			

		String path = "";
		for (int i = 0; i < args.length; i++) {
			if (args[i].length() > 2)
				path = args[i];
		}

		if (path.length() < 2) {
			System.err.println("Shuffle.class [-r][-u] dirpath");
			System.exit(1);
		}

		File folder = new File(path);
		File[] listOfFiles = folder.listFiles();
		if (listOfFiles == null) {
			System.err.println("Bad path");
			System.exit(2);
		}

		if (untag) {
			untag(folder);
			System.exit(0);
		}

		shuffle(folder);

		System.out.println("Shuffle finished !");

	}

	private void untag(File folder) {
		File[] listOfFiles = folder.listFiles();
		for (int i = 0; i < listOfFiles.length; i++) {
			File original = listOfFiles[i];
			if (original.isFile()) {
				String name = original.getName();
				if (name.toLowerCase().endsWith(".mp3")) {
					if (name.startsWith(separator))
						name = name.substring(name.indexOf(separator, 1) + 1);
					if (rename(original, name))
						System.out.println("Untagged " + name);
				}
			}else if (original.isDirectory() && recursive)
				untag(original);
		}

	}

	private void shuffle(File folder) {

		File[] listOfFiles = folder.listFiles();
		shuffleArray(listOfFiles);
		for (int i = 0; i < listOfFiles.length; i++) {
			File original = listOfFiles[i];
			if (original.isFile()) {
				String name = original.getName();
				if (name.toLowerCase().endsWith(".mp3")) {
					if (name.startsWith(separator))
						name = name.substring(name.indexOf(separator, 1) + 1);
					if (rename(original, separator
							+ norm(i, listOfFiles.length) + separator + name))
						System.out.println("Shuffled " + name);
				}
			} else if (original.isDirectory() && recursive)
				shuffle(original);

		}

	}

	private boolean rename(File original, String name) {
		File renamed = new File(original.getParent() + "\\" + name);
		if (original.renameTo(renamed))
			return true;
		else
			return false;

	}

	private String norm(int i, int length) {
		String ret = String.valueOf(i);
		while (ret.length() < String.valueOf(length).length())
			ret = "0" + ret;
		return ret;
	}

	public static void main(String[] args) {
		new Shuffle(args);
	}

	public static void shuffleArray(Object[] a) {
		int n = a.length;
		Random random = new Random();
		random.nextInt();
		for (int i = 0; i < n; i++) {
			int change = i + random.nextInt(n - i);
			swap(a, i, change);
		}
	}

	private static void swap(Object[] a, int i, int change) {
		Object helper = a[i];
		a[i] = a[change];
		a[change] = helper;
	}

}
